package com.mdxq.mall.product.service.impl;

import com.mdxq.mall.product.entity.ProductAttributeCategory;
import com.mdxq.mall.product.mapper.ProductAttributeCategoryMapper;
import com.mdxq.mall.product.mapper.ProductCategoryMapper;
import com.mdxq.mall.product.service.ProductAttributeCategoryService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产品属性分类表 服务层实现。
 *
 * @author ShuJie
 * @since 2025-01-12
 */
@Service
public class ProductAttributeCategoryServiceImpl extends ServiceImpl<ProductAttributeCategoryMapper, ProductAttributeCategory>  implements ProductAttributeCategoryService{

    @Override
    public boolean updateName(Long id, String name) {
        ProductAttributeCategory category=mapper.selectOneById(id);
        category.setName(name);
        mapper.update(category);
        return true;
    }

    @Override
    public boolean save(String name) {
        ProductAttributeCategory category=new ProductAttributeCategory();
        category.setName(name);
        mapper.insert(category);
        return true;
    }
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<ProductAttributeCategory> listWithChildren() {
        List<ProductAttributeCategory>list=list();
        QueryWrapper queryWrapper=new QueryWrapper();
        for(ProductAttributeCategory category:list){
            queryWrapper.eq("parent_id",category.getId());
            category.setProductAttributeList(productCategoryMapper.selectListByQuery(queryWrapper));
            queryWrapper.clear();
        }
        return list;
    }
}
